package jdbc.jdbc_Text;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo_ResultSet_Text {
    public static void main(String[] args) throws SQLException {
        /**
         * 需求:实现查询表的数据,封装为Account对象,并存储在arraylist集合中
         */
        //注册驱动...
        //2.获取连接
        Connection connection = DriverManager
                .getConnection("jdbc:mysql:///学生库?useSSL=false", "root", "mysql");
        //3.定义sql查询语句
        String sql="select * from student";
        //获取sql执行对象
        Statement statement = connection.createStatement();
        //执行sql语句
        ResultSet resultSet = statement.executeQuery(sql);
        //遍历获取数据
        //创建集合
        List<Account> list=new ArrayList<>();
        while (resultSet.next()){
            String sno = resultSet.getString("sno");
            String name = resultSet.getString("sname");
            String ssex = resultSet.getString("ssex");
            //创建封装对象
            Account account=new Account();
            account.setId(sno);
            account.setName(name);
            account.setMoney(ssex);
            //添加到集合中
            list.add(account);
            //打印集合

        }
        System.out.println(list);
        //释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
//创建封装类
class Account{
    private String id;
    private String name;
    private String money;

    public Account() {
    }

    public Account(String id, String name, String money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    /**
     * 获取
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return money
     */
    public String getMoney() {
        return money;
    }

    /**
     * 设置
     * @param money
     */
    public void setMoney(String money) {
        this.money = money;
    }

    public String toString() {
        return "Account{id = " + id + ", name = " + name + ", money = " + money + "}";
    }
}

